﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Models.DataAccess;
using IndoReputation.Models.BusinessObject.Collections;
using IndoReputation.Models.Constants;

namespace IndoReputation.Models.BusinessLogic
{
    public class UsersBOL
    {
        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 4/22/2012  6:03 PM
        /// Todo: Insert an user to db
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static int Insert(Users user)
        {
            try
            {
                user.CreateAt = DateTime.Now;
                user.CreateBy = MySession.UserID;
                user.UpdateAt = null;
                user.UpdateBy = null;
                return UsersDAL.Insert(user);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  9:45 AM
        /// Todo: Update a User Exists in Database 
        /// </summary>
        /// <param name="user">A user.</param>
        /// <returns></returns>
        public static bool Update(Users user)
        {
            try
            {
                user.UpdateAt = DateTime.Now;
                user.UpdateBy = MySession.UserID;
                return UsersDAL.Update(user);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 12/8/2011  2:02 PM
        /// Todo: update login time
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static bool UpdateLoginTime(Users user)
        {
            try
            {
                return UsersDAL.UpdateLoginTime(user);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 12/8/2011  2:02 PM
        /// Todo: update password
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static bool UpdatePassword(Users user)
        {
            try
            {
                return UsersDAL.UpdatePassword(user);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 12/8/2011  2:02 PM
        /// Todo: update active and admin
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static bool UpdateActAdmin(Users user)
        {
            user.UpdateAt = DateTime.Now;
            user.UpdateBy = MySession.UserID;
            return UsersDAL.UpdateActAdmin(user);
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 12/8/2011  2:02 PM
        /// Todo: update lock many users
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static bool UpdateLockMany(bool status, string Ids)
        {
            try
            {
                return UsersDAL.UpdateLockMany(status, Ids);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:36 AM
        /// Todo: Delete a User Existst in Database
        /// </summary>
        /// <param name="user">A user.</param>
        /// <returns></returns>
        public static bool Delete(int userID)
        {
            try
            {
                return UsersDAL.Delete(userID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:36 AM
        /// Todo: Delete a User Existst in Database
        /// </summary>
        /// <param name="user">A user.</param>
        /// <returns></returns>
        public static bool DeleteMany(string Ids)
        {
            try
            {
                return UsersDAL.DeleteMany(Ids);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Users
        /// </summary>
        /// <returns></returns>
        public static UsersList GetAllUsers()
        {
            try
            {
                return UsersDAL.GetAllUsers() ?? new UsersList();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Users
        /// </summary>
        /// <returns></returns>
        public static List<Users> GetAllUsersAdmin()
        {
            try
            {
                return UsersDAL.GetAllUsersAdmin() ?? new List<Users>();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:40 AM
        /// Todo: Select a User existst in Database
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public static Users GetUserById(int userID)
        {
            try
            {
                return UsersDAL.GetUserById(userID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }


        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 10/2/2012  2:46 PM
        /// Todo: 
        /// </summary>
        /// <param name="loginName">Name of the login.</param>
        /// <param name="memoryWord">The memory word.</param>
        /// <returns></returns>
        public static Users GetUserByNameAndWord(string loginName, string memoryWord)
        {
            try
            {
                return UsersDAL.GetUserByNameAndWord(loginName, memoryWord) ?? new Users();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public static UsersExtend GetUserExtendById(int userID)
        {
            try
            {
                return UsersDAL.GetUserExtendById(userID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

         /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 12/2/2011  3:01 PM
        /// Todo: Check login
        /// </summary>
        /// <param name="loginName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static IRConstants.UserStatus GetUserByNameAndPass(string loginName, string password)
        {
            try
            {
                return UsersDAL.GetUserByNameAndPass(loginName, password);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Users
        /// </summary>
        /// <returns></returns>
        public static UsersJoinList GetAllUsersJoin()
        {
            try
            {
                return UsersDAL.GetAllUsersJoin() ?? new UsersJoinList();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public static UsersCheck GetUserCEOAndEnableById(int userID)
        {
            try
            {
                return UsersDAL.GetUserCEOAndEnableById(userID) ?? new UsersCheck();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:36 AM
        /// Todo: Delete a User Existst in Database
        /// </summary>
        /// <param name="user">A user.</param>
        /// <returns></returns>
        public static bool CheckDuplicateLoginName(string loginName)
        {
            try
            {
                return UsersDAL.CheckDuplicateLoginName(loginName);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

    }
}